discard
	
set more off 

 use "$healthsave\cleaned prevalence_w7.dta", clear 

/** coding up onsets **/	

	** for most conditions this is trivial 
	** (simply the difference in the prevalence) 

	** for heart attacks, strokes and cancer, 
	** this is less trivial. 
	
	lab define new 		-2 "Not in prev wave" ///
						-1 "Missing" ///
						 0 "No new diagnosis" ///
						 1 "New diagnosis" 
		
		

		tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
				 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
				 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
				 HeartRhythm Diabetes Stroke Cholesterol	
		
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			forv i = 0/7 { 
			
			gen new`x'_w`i' = .
			lab var new`x'_w`i' "Diagnosed with `1' since previous wave" 
			
			}
		mac shift 
		}
		
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 		
		forv i = 2/7 { 
			local Pi = `i'-1 
			
			replace new`x'_w`i' = 1 ///
				if ever`x'_w`i' == 1 & ever`x'_w`Pi' == 0 
			
			replace new`x'_w`i' = 0 ///
				if ever`x'_w`i' == 1 & ever`x'_w`Pi' == 1 
				
			replace new`x'_w`i' = 0 ///
				if ever`x'_w`i' == 0 
				
			replace new`x'_w`i' = -1 /// 
				if alive_w`i' == 1 & ///
				(ever`x'_w`i' == -1  | ever`x'_w`Pi' == -1) 
				
			replace new`x'_w`i' = -2 if alive_w`Pi' !=1 & alive_w`i' == 1 			
			lab val new`x'_w`i' new 
		} 
		}
		
	
		
		foreach x in mi ca st { 
			forv i = 1/7 { 
				
				gen onset`x'_w`i' = new`x'_w`i' 
				lab var onset`x'_w`i' "Reports recent onset of `x'" 
				lab val onset`x'_w`i' onset 
			}
		}	
		
		
		lab define onset 	  -2 "Not in prev wave" /// 
							  -1 "Missing" 				///
							   0 "No onset reported" 		///
							   1 "Onset reported" 		, replace
		
	/** 'how cleaned' variable for onsets **/	
	
		lab define howclnew 0  "from first diagnosis" 	///
							1  "from new report in that wave" ///
							2  "from retrospective report"    ///
							3  "possible double counting" 
		
		
		tokenize HeartAttack Cancer Stroke 
		foreach x in mi ca st {
		forv i = 1/7 { 
		
			gen howclnew`x'_w`i' = . 
			lab var howclnew`x'_w`i' "How new diagnoses of `1' cleaned" 
			lab val howclnew`x'_w`i' howclnew 
			
			replace howclnew`x'_w`i' = 0 if new`x'_w`i' == 1 
		} 
			mac shift 	
		}
		
		
	/** so far we have coded up the FIRST onset. 
		now, for cancer/stroke/heart attacks, we would like to 
		find any onsets AFTER the first onset **/
		
		foreach x in ca mi st { 
		forv i = 2/7 { 
			replace onset`x'_w`i' = new`x'_w`i'
		} 
		}
		
		forv i = 2/7 { 
			local Pi = `i' - 1 
			
			replace howclnewmi_w`i' = 1 if henmmiw`i'  >=1 & henmmiw`i' <=3 ///
						& evermi_w`i' == 1 & newmi_w`i'!=1 & inwave`Pi'!=0 
			
			replace onsetmi_w`i' = 1 if henmmiw`i' >=1 & henmmiw`i' <=3 ///
						& evermi_w`i' == 1 & inwave`Pi'!=0 
		
			replace howclnewst_w`i' = 1 if henmstw`i' >=1 & henmstw`i' <=3 ///
						& everst_w`i' == 1 & newst_w`i'!=1  & inwave`Pi'!=0 
		
			replace onsetst_w`i' = 1 if henmstw`i' >=1 & henmstw`i' <=3 ///
						& everst_w`i' == 1  & inwave`Pi'!=0 
						
			forv  j = 2/7 { 			
			if `i' <= `j' {
			
			if `i' < `j' { 
			replace howclnewmi_w`i' = 2 /// 
						if onsetdatemi`j' < intdate_w`i' ///
						& onsetdatemi`j' >= intdate_w`Pi' ///
						& evermi_w`i' == 1 ///
						& alive_w`i' == 1  ///
						& newmi_w`i'!=1 
			
			replace howclnewst_w`i' = 2 /// 
						if onsetdatest`j' < intdate_w`i' ///
						& onsetdatest`j' >= intdate_w`Pi' ///
						& everst_w`i' == 1 ///
						& alive_w`i' == 1 	///
						& newst_w`i' !=1 
			
			replace howclnewca_w`i' = 2 /// 
						if onsetdateca`j' < intdate_w`i' ///
						& onsetdateca`j' >= intdate_w`Pi' ///
						& everca_w`i' == 1 ///
						& alive_w`i' == 1 	///
						& newca_w`i' !=1 
			}
			
			else { 
			replace howclnewmi_w`i' = 1 /// 
						if onsetdatemi`j' < intdate_w`i' ///
						& onsetdatemi`j' >= intdate_w`Pi' ///
						& evermi_w`i' == 1 ///
						& alive_w`i' == 1  ///
						& newmi_w`i'!=1 		
						
			replace howclnewst_w`i' = 1 /// 
						if onsetdatest`j' < intdate_w`i' ///
						& onsetdatest`j' >= intdate_w`Pi' ///
						& everst_w`i' == 1 ///
						& alive_w`i' == 1  ///
						& newst_w`i'!=1 		

			replace howclnewca_w`i' = 1 /// 
						if onsetdateca`j' < intdate_w`i' ///
						& onsetdateca`j' >= intdate_w`Pi' ///
						& everca_w`i' == 1 ///
						& alive_w`i' == 1  ///
						& newca_w`i'!=1 								
			}
			
			replace onsetmi_w`i' = 1 /// 
						if onsetdatemi`j' < intdate_w`i' ///
						& onsetdatemi`j' >= intdate_w`Pi' ///
						& evermi_w`i' == 1 & henmmiw`i'!=0 ///
						& alive_w`i' == 1 					
			
			replace onsetst_w`i' = 1 /// 
						if onsetdatest`j' < intdate_w`i' ///
						& onsetdatest`j' >= intdate_w`Pi' ///
						& everst_w`i' == 1 & henmstw`i'!=0 ///
						& alive_w`i' == 1 		
			
			replace onsetca_w`i' = 1 	///
						if onsetdateca`j' < intdate_w`i' ///
						& onsetdateca`j' >= intdate_w`Pi' ///
						& everca_w`i' == 1 ///
						& alive_w`i' == 1 
						
				}
			}
		}
		
		
		
		
		
		
		** might be concerned about double counting onsets. 
		** check everyone with more than one onset reported 
		
	forv i = 2/7 { 
	foreach x in ca mi st { 
		replace onset`x'_w`i' = 1 if new`x'_w`i'== 1
	}
	}		
		
		
/** CANCER **/	
		
		**preserve 
		forv i = 2/7 { 
			replace onsetca_w`i' = . if onsetca_w`i' <0  
		}
		egen totonsets_ca = rowtotal(onsetca_w1 onsetca_w2 onsetca_w3 onsetca_w4 onsetca_w5 onsetca_w6 onsetca_w7)
		ta totonsets_ca 
		**restore 	
		
		
		/** CANCER **/
		/** slightly concerned about the following obs: **/
			
			/** idauniq == 106020 
				gives an onset date of October 2007 in w4, 
				then October 2008 in w6. (seems similar dates 
				and retrospective report). Cancer diagnosis not 
				ff to w6. 'Somewhere else' given in w4, w5 and w6 
				for where the cancer first started. **/
				
			/** idauniq == 113078 
				in w3 reports onset in November 2005, then in w6 reports 
				onset in November 2006. Again, a delayed report 
				with possible confusion about when the onset was? 
				First time the type of cancer is given as 'Lymphoma', 
				the second time as 'Leukaemia'.**/ 
				
			/** idauniq == 160837 
				reports 3 onsets March 2010, September 2010 and October 2012
				*/

/** STROKE **/	
				
	gen totonsets_st = (onsetst_w2==1)+(onsetst_w3==1)+(onsetst_w4==1)+(onsetst_w5==1)+(onsetst_w6==1)
	gen totstrokes    = henmstw2 if henmstw2!=. & henmstw2>0 
		forv i = 3/7 { 
			replace totstrokes = totstrokes + henmstw`i' if henmstw`i' !=. & henmstw`i' >0 & totstrokes!=. 
			replace totstrokes = henmstw`i' if henmstw`i' !=. & totstrokes==. & henmstw`i' >0
		}
		
		forv i = 2/7 { 
		local Pi = `i' 
		
		replace howclnewst_w`i' = 3 if ///
				onsetst_w`i' == 1 ///
				& henmstw`i' == 1  ///
				& totstrokes< totonsets_st & totonsets_st !=. ///
				& onsetdatest`i' <intdate_w`Pi' & intdate_w`Pi'!=. 
		
		replace onsetst_w`i' = 0 		///	
				if henmstw`i' == 1  ///
				& totstrokes< totonsets_st & totonsets_st !=. ///
				& onsetdatest`i' <intdate_w`Pi' & intdate_w`Pi'!=. 
		}
		

/** HEART ATTACK **/ 

	gen totonsets_mi = (onsetmi_w2==1)+(onsetmi_w3==1)+(onsetmi_w4==1)+(onsetmi_w5==1)+(onsetmi_w6==1)
	gen tothattas    = henmmiw2 if henmmiw2!=. & henmmiw2>0 
		forv i = 3/7 { 
			replace tothattas = tothattas + henmmiw`i' if henmmiw`i' !=. & henmmiw`i' >0 & tothattas!=. 
			replace tothattas = henmmiw`i' if henmmiw`i' !=. & tothattas==. & henmmiw`i' >0
		}
		
		
	/* idauniq = 103799 reports heart attack in w6, but gives first date of onset 
		just after w2 interview (after reporting heart attack in wave 2.) */ 
		
		forv i = 2/7 { 
		local Pi = `i' 

		replace howclnewmi_w`i' = 3 if ///
				onsetmi_w`i' == 1 		///	
				& henmmiw`i' == 1  ///
				& tothattas< totonsets_mi & totonsets_mi !=. ///
				& onsetdatemi`i' <intdate_w`Pi' & intdate_w`Pi'!=. 		
		
		replace onsetmi_w`i' = 0 		///	
				if henmmiw`i' == 1  ///
				& tothattas< totonsets_mi & totonsets_mi !=. ///
				& onsetdatemi`i' <intdate_w`Pi' & intdate_w`Pi'!=. 
		
	}

		
   save "$healthsave\prevalence and onsets_w7.dta", replace
   